//
//  HACHaloLayer.m
//  HStockCharts
//
//  Created by Hotacool on 2018/10/26.
//

#import "HACHaloLayer.h"

@interface HACHaloLayer ()
@property (nonatomic, strong) CAAnimationGroup *animationGroup;
@property (nonatomic, strong) CALayer *haloLayer;
@end


@implementation HACHaloLayer

- (id)init {
    self = [super init];
    if (self) {
        
        
        self.haloLayer = [CALayer layer];
        self.contentsScale = [UIScreen mainScreen].scale;
        self.haloLayer.contentsScale = [UIScreen mainScreen].scale;
        //        self.opacity = 0;
        
        // default
        self.radius = 60;
        self.animationDuration = 2;
        self.pulseInterval = 0;
        self.backgroundColor = [[UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1] CGColor];
        self.haloLayer.backgroundColor = [UIColor redColor].CGColor;
        [self insertSublayer:self.haloLayer below:self];
        
        [self setDelegate:(id<CALayerDelegate>)self];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
            
            [self setupAnimationGroup];
            
            if(self.pulseInterval != INFINITY) {
                
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    
                    [self.haloLayer addAnimation:self.animationGroup forKey:@"pulse"];
                });
            }
        });
    }
    return self;
}

- (void)setRadius:(CGFloat)radius {
    
    _radius = radius;
    
    CGPoint tempPos = self.position;
    
    CGFloat diameter = self.radius * 2;
    
    self.bounds = CGRectMake(0, 0, diameter, diameter);
    self.cornerRadius = self.radius;
    self.position = tempPos;
    self.haloLayer.bounds = CGRectMake(CGRectGetMidX(self.frame),  CGRectGetMidY(self.frame), diameter * 3, diameter * 3);
    
    self.haloLayer.cornerRadius = self.radius*3;
    self.haloLayer.position = tempPos;
    self.haloLayer.backgroundColor = self.backgroundColor;
    
}


- (void)setupPosition:(CGPoint)position
{
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.position = position;
    self.haloLayer.position = CGPointMake(CGRectGetWidth(self.bounds)/2,CGRectGetHeight(self.bounds)/2);
    [CATransaction commit];
}

- (void)setupAnimationGroup {
    
    CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    
    self.animationGroup = [CAAnimationGroup animation];
    self.animationGroup.duration = self.animationDuration + self.pulseInterval;
    self.animationGroup.repeatCount = INFINITY;
    self.animationGroup.removedOnCompletion = NO;
    self.animationGroup.timingFunction = defaultCurve;
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
    scaleAnimation.fromValue = @0.0;
    scaleAnimation.toValue = @1.0;
    scaleAnimation.duration = self.animationDuration;
    scaleAnimation.delegate = (id<CAAnimationDelegate>)self;
    
    CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.duration = self.animationDuration;
    opacityAnimation.values = @[@1.0, @0.45, @0];
    opacityAnimation.keyTimes = @[@0, @0.5, @1];
    opacityAnimation.removedOnCompletion = NO;
    opacityAnimation.delegate = (id<CAAnimationDelegate>)self;
    NSArray *animations = @[scaleAnimation, opacityAnimation];
    
    self.animationGroup.animations = animations;
}

- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"did start");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"did stop");
}


@end
